<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>{$id}号位展位预定</title>
		<link rel="stylesheet" href="__TMPL__Public/css/normalize.css" />
		<link rel="stylesheet" href="__TMPL__Public/css/main.css" />
		<style type="text/css">
			body,form{overflow-x: hidden;}
		</style>
	</head> 
	<body>
		<!--<div class="header">
			3号位展位预定
		</div>--> 
		<form id="adorder" action="/HAction/order_post" method="post">
			<input type="hidden" name="modify" value="{$modify}"/>
			<input type="hidden" name="user_uniq_id" value="{$user.user_uniq_id}"/>
			<input type="hidden" name="status" value="1"/>
			<if condition="!empty($ad)">
				<input type="hidden" name="stand_id" value="{$order.id}"/>
				<input type="hidden" id="spend" name="spend" value="{$order.spend}"/>
				<input type="hidden" id="dis_name" name="discount_name" value="{$order.discount_name}"/>
				<input type="hidden" id="dis_rate" name="discount_rate" value="{$order.discount_rate}"/>
				<else/>
				<input type="hidden" name="stand_id" value="{$order.id}"/>
				<input type="hidden" id="spend" name="spend" value=""/>
				<input type="hidden" id="dis_name" name="discount_name" value=""/>
				<input type="hidden" id="dis_rate" name="discount_rate" value=""/>
			</if>
		<div class="linebg">
		  <if condition="$hidead eq 0">
			<div class="line-i bb">
				<span class="line-before more-w">费用说明</span>
				<span class="line-after">{$order.change_day}金钱<span>/天</span></span>
			</div>
			<div class="line-i bb">
				<span class="line-before more-w">折扣优惠</span>
				<span class="line-after">
					<if condition="!empty($ad)">
						<select id="discount" disabled="disabled">
							<option data-value="{$ad[0].discount_rate}" data-day="{$ad[0].discount_name}">
									{$ad[0].discount_rate}折<span style="font-size: 12px;">/{$ad[0].discount_name}天</span>
								</option>
						</select>
						<else/>
						<select id="discount">
							<volist name="discount" id="v">
								<option data-value="{$v.pvalue}" data-day="{$v.pname}">
									{$v.pvalue}折<span style="font-size: 12px;">/{$v.pname}天</span>
								</option>
							</volist>
						</select>
					</if>
				</span>
			</div>
      </if>
			<div class="line-i">
				<span class="line-before more-w">截至时间</span>
				<span class="line-after"><input type="date" readonly name="expire_date" id="expire_date" value="" /></span>
			</div>
		</div>
		<div class="linebg">
			<div class="line-i">
				<if condition="!empty($ad)">
					<input type="text" name="link_url" id="link_url" value="{$ad[0].link_url}" placeholder="请输入展位链接地址"/>
					<else/>
					<input type="text" name="link_url" id="link_url" value="" placeholder="请输入展位链接地址"/>
				</if>
				
			</div>
		</div>
		<div class="linebg">
			<div class="line-i bb">
				<if condition="!empty($ad)">
					<input type="text" name="link_name" id="link_name" value="{$ad[0].link_name}" placeholder="请输入展位链接地址"/>
					<else/>
					<input type="text" name="link_name" id="link_name" value="" placeholder="请输入展位名字"/>
				</if>
			</div>
			<div class="line-i">
				<if condition="!empty($ad)">
					<textarea name="mark" id="mark" placeholder="简介备注说明">{$ad[0].mark}</textarea>
					<else/>
					<textarea name="mark" id="mark" placeholder="简介备注说明"></textarea>
				</if>
			</div>
		</div>
		
		<div class="linebg">
			<div class="line-i bb">
					<input type="text" name="contact"   placeholder="请输入联系人"/>
			</div>
			<div class="line-i bb">
					<input type="text" name="mobile" placeholder="联系电话"/>
			</div>
			<div class="line-i bb">
					<input type="text" name="qqwechat"   placeholder="qq 或微信"/>
			</div>
			<div class="line-i">
					<input type="text" name="addr"   placeholder="联系地址"/>
			</div>
		</div>
		<div class="linebg">
			<div class="line-i bb">
				<span class="line-before">展位图</span>
				<span class="line-after noclr"><span>（第一张为封面）</span></span>
			</div>
			<div id="iptbox" class="line-i ipt">
				<div id="addpic" class="iptbox">
					<label for="ippic" style="display: inline-block;line-height: 0;margin-bottom: 15px;">
						<img id="addimg" src="__TMPL__Public/img/addpic.jpg"/>
					</label>
				</div>
				<div class="iptbox"></div>
				<input name="file" onchange="addiptbox()" id="ippic" style="display: none;" type="file" accept="image/*"/>
				<input name="file" onchange="editimg()" id="editpic" style="display: none;" type="file" accept="image/*"/>
				<for start="0" end="$adnum">
					<if condition="($i+1)%4 eq 0">
						<label for="editpic" data-index="{$i}" onclick="setedit(this)" class="warp iptbox"><img src="{$ad[$i].icon}"/></label>
						<else/>
						<label for="editpic" data-index="{$i}" onclick="setedit(this)" class="iptbox"><img src="{$ad[$i].icon}"/></label>
					</if>
				</for>
			</div>
		</div>
		<div class="btn-footer">
		  <if condition="$hidead eq 0">
			<div>共计：<span>金钱:<span id="needmny"></span></span></div></if>
			<div>
				<button id="submitbtn" type="button" onclick="submitorder()">提交申请</button>
			</div>
			<div style="margin-top: 0px;">
				<button type="button" style="width: 60px;" onclick="gotos()">帮助</button>
			</div>
		</div>
		</form>
		<div id="tip" class="dialog">
			<div class="dialogbg">
				<div class="dlg-title">提示</div>
				<div class="dlg-content">您已经申请了{$order.id}号位，我们已经为你锁定了该展位，后台通过审核后悔立即显示，若审核失败，所扣金钱将返还到您的账户</div>
				<button class="dlg-btn">确 定</button>
			</div>
		</div>
		<div class="footerbody"></div>
	</body>
	<script src="__TMPL__Public/js/jquery-1.11.0.js"></script>
	<script src="__TMPL__Public/js/main.js?{$version}" ></script>
	<script src="__TMPL__Public/js/ajaxfileupload.js"></script>
	<script type="text/javascript">
		var modify = "{$modify}";
		var standvalue = "{$order.change_day}";
		(function(){
			document.title = "{$id}号位展位预定";
			$(".iptbox img").each(function(){
				$(this).height($(this).width());
			});
			$("#discount").change(function(){
				var ds = $(this).find("option:selected"); 
				var dt= ds.attr("data-value");
				var dd= ds.attr("data-day");
				countmoney(dt,dd);
			});
			var discountselect = $("#discount").find("option:selected"); 
			var discount= discountselect.attr("data-value");
			var discountday= discountselect.attr("data-day");
			countmoney(discount,discountday);
		})();
		function countmoney(discount,discountday){
			var money = discount*discountday*standvalue;
			$("#needmny").text(money);
			$("#dis_name").val(discountday)
			$("#dis_rate").val(discount);
			setexpire(discountday);
			$("#spend").val(money)
		}
		function setexpire(daynum){
			var dd = new Date();
			dd.setTime(dd.getTime()+daynum*86400000);
			var year = dd.getFullYear();
			var tomounth = dd.getMonth();
			var day= dd.getDate();
//			var today = yeadr+"-"+formatdate(tomounth)+"-"+formatdate(day);
			var mm =parseInt(tomounth)+1; 
			var mouth = formatdate(mm);
			var day = formatdate(day);
			$("#expire_date").val(year+"-"+mouth+"-"+day);
		}
		function formatdate(val){
			if((val+"").length==1)
				return "0"+val;
			else return val;
		}
		function gotos(){
			location.href="/note.html";
		}
	
	var imglen= parseInt("{$adnum}");
	var balance = "{$user.balance}";
	toast.init();
	var _load = new loader();
		_load.init("/themes/simplebootx_mobile/Public/img/loading.gif");
		var dg = new dialog("tip");
		var submiting = false;
		function submitorder(){
			if(submiting)return;
//			console.log($("#addpic").children().length);
			if(imglen<=0){
				toast.show("请上传要展示的图片");return false;
			}
			if(!IsURL($("#link_url").val())){
				toast.show("链接格式不正确");
				return false;
			}
			if($("#link_name").val().length==0){
				toast.show("请输入展位名字");return false;
			}
			if(parseInt(balance)<$("#spend").val()&&modify!="1"&&hidead==0){toast.show("余额不足 请充值");return;}
			submiting = true;
			$("#submitbtn").text("提交中...");
			$("#adorder").submit();
		}
		var hidead = "{$hidead}";
		var iptbox = $("#iptbox");
		var uping = false; 
		function addiptbox(){
			if(uping)return;
			uping = true;
			upfile(
				function(data){
					var nodeh="";
					if(iptbox.children(".iptbox").length%5==0){
						nodeh = '<label style="height:0;" for="editpic" data-index="'+imglen+
						'" onclick="setedit(this)" class="iptbox warp"><img src="'+data+'"/></label>';
					}
					else nodeh = '<label style="height:0;" for="editpic" data-index="'+imglen+
					'" onclick="setedit(this)" class="iptbox"><img src="'+data+'"/></label>';
					iptbox.append(nodeh);
					var ipchild= iptbox.children(".iptbox:last-child");
					ipchild.css("max-height",addimg.width());
					ipchild.animate({"height":addimg.width()},1000,"swing");
					uping = false;
				}
			);
		}
		var _edimg = null;
		function setedit(t){
			_edimg = t;
		}
		function editimg(){
			editfile($(_edimg));
		}
		
	var addimg = $("#addimg");
	function upstart(inputimg){
		if(inputimg){
			inputimg.addClass("uping");
			inputimg.attr("src","/themes/simplebootx_mobile/Public/img/loading.gif");
		}else{
			addimg.addClass("uping");
			addimg.attr("src","/themes/simplebootx_mobile/Public/img/loading.gif");
		}
		
	}
	function upend(inputimg){
		if(inputimg){
			inputimg.removeClass("uping");
			inputimg.attr("src","/themes/simplebootx_mobile/Public/img/addpic.jpg");
		}else{
			addimg.removeClass("uping");
			addimg.attr("src","/themes/simplebootx_mobile/Public/img/addpic.jpg");
		}
	}
	
		function upfile(callback){
			upstart();
			$.ajaxFileUpload({
	            url:'/api/AppGroup/image_upload/',//处理图片脚本
	            secureuri :false,
	            fileElementId :'ippic',//file控件id
	            dataType : 'content',
	            success:function (data, status){
			        var dt = ajaxStrToObj(data);
			        callback(dt.data);
			        $.post("/HAction/upimg",{"url":dt.data,"index":imglen,"modify":modify},
			        	function(data){
			        		imglen++;
			        		console.log("index:"+imglen);
			        	},"json");
			        addimg.removeClass("uping");
			        upend();
	            },
	            error:function(data, status, e){
	                console.log(e);
	                upend();
	            }
	    	});
		}
		function editfile(_input){
			upstart(_input.children());
			$.ajaxFileUpload({
	            url:'/api/AppGroup/image_upload/',//处理图片脚本
	            secureuri :false,
	            fileElementId :'editpic',//file控件id
	            dataType : 'content',
	            success:function (data, status){
			        var dt = ajaxStrToObj(data);
			        $.post("/HAction/upimg",{"url":dt.data,"index": _input.attr("data-index")},
			        	function(){
			        		var _intch = _input.children();
			        		_intch.attr("src",dt.data);
			        		_intch.height(_intch.width());
			        	},"json");
			        	upend(_input.children());
	            },
	            error:function(data, status, e){
	            	upend(_input.children());
	                console.log(e);
	            }
	    	});
		}
	</script>
</html>
